package ${pkgName };

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.ld.dao.model.PageNavigationBean;
import com.ld.dao.model.QueryModel;
import com.ld.mvc.annotation.Annotation.ActionName;
import com.ld.mvc.annotation.Annotation.MethodName;
import com.ld.util.CreditManageWebUtil;
import com.model.Static;
import com.util.CmUtil;
import lombok.extern.slf4j.Slf4j;
import com.ld.action.commonAction.CreditManageBaseAction;

import ${modelPackage!}.${modelName};
<% //自定义service %>
<% if ( isNotEmpty(serviceName) ) { %>
import ${servicePackage!}.${serviceName};
<% } %>


/**
*
* ${modelComments}
*
* @ClassName ${className}
<% if ( isNotEmpty(email) ) { %>
* @author <a href="mailto:${email}" target="_blank">${author!"无名氏"}</a>
<% } else { %>
* @author ${author!"无名氏"}
<% } %>
* @date ${nowTime}
*
*/

@Slf4j
@ActionName("/${baseURL}")
public class ${className} extends CreditManageBaseAction {
    <% //自定义service %>
    <% if ( isNotEmpty(serviceName) ) { %>
    @Autowired
    protected ${serviceName} ${serviceObjName};
    <% } %>


    @MethodName("toSearch")
    public void toSearch(String... args) throws ServletException, IOException{
        CreditManageWebUtil.forward(request, response, "${jspDir!}/${jspPrefix!}_search.jsp");
    }

    @MethodName("search")
    public void search(String... args) throws ServletException, IOException{
        QueryModel queryModel =new QueryModel();
        queryModel.addEq("state", Static.STATE_NORMAL);
        PageNavigationBean<${modelName}> pageBean = MYSQL_BASE_DAO.getPageNavigationBean(${modelName}.class, queryModel, pageNum,PAGE_SIZE);
        request.setAttribute("pageBean", pageBean);
        CreditManageWebUtil.forward(request, response, "${jspDir!}/${jspPrefix!}_list.jsp");
    }

    @MethodName("show")
    public void show(String... args) throws ServletException, IOException{
        long id =getLong("id");
        ${modelName} model=MYSQL_BASE_DAO.findById(${modelName}.class,id);
        request.setAttribute("model",model);
        CreditManageWebUtil.forward(request, response, "${jspDir!}/${jspPrefix!}_show.jsp");
    }

    @MethodName("/toAddOrUpdate")
    public void toAddOrUpd(String... args) throws ServletException, IOException{
        long id =getLong("id");
        ${modelName} model=MYSQL_BASE_DAO.findById(${modelName}.class,id);
        request.setAttribute("model",model);
        CreditManageWebUtil.forward(request, response, "${jspDir!}/${jspPrefix!}_update.jsp");
    }

    @MethodName("/addOrUpdate")
    public void addOrUpd(String... args) throws ServletException, IOException{
        ${modelName} model=getModel(${modelName}.class);
        if(model.getId()==null || model.getId()==0L ){//添加
            long id = CmUtil.getPkId();
            model.setId(id);
            boolean flag= MYSQL_BASE_DAO.save(model);
            if(flag){
                CreditManageWebUtil.sendOk(request, response, "添加成功！");
            }else{
                CreditManageWebUtil.sendError(request, response, "添加失败！");
            }
        }else{//修改
            model.setUPDATETIME(new Date());
            boolean flag= MYSQL_BASE_DAO.update(model);
            if(flag){
                CreditManageWebUtil.sendOk(request, response, "修改成功！");
            }else{
                CreditManageWebUtil.sendError(request, response, "修改失败！");
            }
        }

    }

    @MethodName("/del")
    public void del(String... args) throws ServletException, IOException{
        Long id =getLong("id");
        if(id!=null || id!= 0){
            boolean flag= MYSQL_BASE_DAO.delete(${modelName}.class,id);
            if(flag){
                CreditManageWebUtil.sendOk(request, response, "删除成功！");
            }else{
                CreditManageWebUtil.sendError(request, response, "删除失败！");
            }
        }else{
            CreditManageWebUtil.sendError(request, response, "删除的凭据（id）为空！");
        }
    }

}